import com.google.gson.GsonBuilder;
import model.GsonObject1;
import test_net.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GsonTest {
    public static void main(String[] args) {

        HttpResult<List<ListBean>> listHttpResult = new TestHttpListCallback<ListBean>() {
        }.convert(DataSource.INSTANCE.getListData1());

        System.out.println(listHttpResult);

        for (ListBean bean : listHttpResult.data) {
            System.out.println(bean.name);
        }
        HttpResult<GsonObject2> result = new TestHttpCallback<GsonObject2>() {
        }.convert(DataSource.INSTANCE.getObjectData1());

        System.out.println(result.data);

        HttpResult<Map<String, GsonObject1>> result1 = new TestMapHttpCallback<String, GsonObject1>() {
        }.convert(DataSource.INSTANCE.getMapData1());
        System.out.println(result1);
        for (Map.Entry<String, GsonObject1> entry : result1.data.entrySet()) {
            System.out.println("key=" + entry.getKey() + "\tvalue=" + entry.getValue());
        }
        Map<GsonObject1, String> map = new LinkedHashMap<>();
        map.put(new GsonObject1(1, "3车道", "东周考场"), "aa");
        map.put(new GsonObject1(2, "4车道", "东周考场"), "bb");
        map.put(new GsonObject1(3, "5车道", "东周考场"), "cc");
        HttpResult<Map<GsonObject1, String>> httpResult = new HttpResult<>();
        httpResult.data = map;
        httpResult.msg = "ok";
        httpResult.status = 200;
        String json = new GsonBuilder().enableComplexMapKeySerialization().create().toJson(httpResult);
        System.out.println("======================");
        System.out.println("mapJson:" + json);

//        Type type = new TypeToken<HttpResult<Map<GsonObject1, String>>>() {
//        }.getType();
//        HttpResult<Map<GsonObject1, String>> result2 = new Gson().fromJson(json, type);
//        for (Map.Entry<GsonObject1, String> entry : result2.data.entrySet()) {
//            System.out.println("key=" + entry.getKey() + "\tvalue=" + entry.getValue());
//        }
        //这样反而报错，未解？
//        HttpResult<Map<GsonObject1, String>> convertResult = new TestMapHttpCallback<GsonObject1, String>().convert(json);
//        System.out.println(convertResult);


        //"null",""没办法转成空数组？？？
//        Gson listGson = new GsonBuilder().registerTypeHierarchyAdapter(List.class, new List1TypeAdapter()).create();
//        List<String> listStr = listGson.fromJson("{}", new TypeToken<List<String>>() {
//        }.getType());
//        System.out.println(listStr);
    }
}
